<template>
  <div class="login-container">
    <div class="login-content">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <!-- 系统标题和Logo -->
        <div class="system-brand">
          <div class="logo-container">
            <div class="logo-icon">
              <svg viewBox="0 0 48 48" xmlns="http://www.w3.org/2000/svg">
                <rect width="48" height="48" rx="8" fill="#FF7A00"/>
                <circle cx="24" cy="24" r="16" fill="white"/>
                <circle cx="18" cy="24" r="4" fill="#FF7A00"/>
                <circle cx="30" cy="24" r="4" fill="#FF7A00"/>
              </svg>
            </div>
          </div>
          <h1 class="system-title">短剧个性化推荐系统</h1>
            <p class="system-subtitle">智能推荐，精彩短剧，好剧不错过</p>
        </div>

        <!-- 登录表单 -->
        <div class="form-wrapper">
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="用户名"
              :prefix-icon="'el-icon-user-solid'"
            >
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="handleLogin"
              :prefix-icon="'el-icon-lock'"
            >
            </el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="captchaEnabled">
              <el-input
                v-model="loginForm.code"
                auto-complete="off"
                placeholder="验证码"
                style="width: 63%"
                @keyup.enter.native="handleLogin"
                :prefix-icon="'el-icon-verify'"
              >
              </el-input>
              <div class="login-code">
                <img v-if="codeUrl" :src="codeUrl" class="login-code-img" @click="getCode" title="点击刷新验证码" />
                <div v-else class="code-loading">
                  <i class="el-icon-loading"></i>
                  <span>加载中...</span>
                </div>
                <el-button v-if="!codeUrl" @click="getCode" type="text" class="refresh-btn" title="手动刷新验证码">
                  <i class="el-icon-refresh"></i>
                </el-button>
              </div>
            </el-form-item>
          <el-checkbox v-model="loginForm.rememberMe" class="remember-me">记住密码</el-checkbox>
          <el-form-item style="width:100%;">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              style="width:100%;"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </div>

        <!-- 功能特点区域 -->
        <div class="system-features">
          <div class="feature-item">
            <i class="el-icon-data-line"></i>
            <span>智能数据分析</span>
          </div>
          <div class="feature-item">
            <i class="el-icon-refresh"></i>
            <span>个性化推荐</span>
          </div>
          <div class="feature-item">
            <i class="el-icon-pie-chart"></i>
            <span>数据可视化</span>
          </div>
          <div class="feature-item">
            <i class="el-icon-shield-check"></i>
            <span>安全可靠</span>
          </div>
        </div>
      </el-form>
    </div>
    <!-- 底部 -->
    <div class="el-login-footer">
      <span>Copyright © 2023-2025 短剧个性化推荐系统 版权所有</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入用户名" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      // 显示加载状态
      this.loading = true;
      
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.loginForm.uuid = res.uuid
        }
        this.loading = false;
      }).catch(error => {
        console.error('获取验证码失败，正在重试...', error)
        // 如果第一次请求失败，延迟1秒后重试一次
        setTimeout(() => {
          getCodeImg().then(res => {
            this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
            if (this.captchaEnabled) {
              this.codeUrl = "data:image/gif;base64," + res.img
              this.loginForm.uuid = res.uuid
            }
            this.loading = false;
          }).catch(err => {
            console.error('获取验证码重试失败，请检查后端服务', err)
            this.loading = false;
            // 如果重试也失败，可以显示一个友好的错误提示
            this.$message({
              message: '验证码加载失败，请刷新页面重试或检查网络连接',
              type: 'warning',
              duration: 5000
            })
          })
        }, 1000)
      })
    },
    getCookie() {
      const username = Cookies.get("username")
      const password = Cookies.get("password")
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 })
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove("username")
            Cookies.remove("password")
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
// 登录页面容器
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #fff8f0 0%, #fff 100%);
  position: relative;
  overflow: hidden;
  
  // 添加背景装饰元素
  &::before {
    content: "";
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: radial-gradient(circle, rgba(255,122,0,0.05) 0%, transparent 70%);
    z-index: 0;
  }
}

// 登录内容区域
.login-content {
  position: relative;
  z-index: 1;
  padding: 20px;
}

// 登录表单卡片
.login-form {
  border-radius: 12px;
  background: #ffffff;
  width: 420px;
  padding: 30px;
  box-shadow: 0 8px 32px rgba(255,122,0,0.1);
  transition: all 0.3s ease;
  
  // 悬停效果
  &:hover {
    box-shadow: 0 12px 40px rgba(255,122,0,0.15);
  }
}

// 系统品牌区域
.system-brand {
  text-align: center;
  margin-bottom: 30px;
}

// Logo容器
.logo-container {
  display: flex;
  justify-content: center;
  margin-bottom: 15px;
}

// Logo图标
.logo-icon {
  width: 60px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.3s ease;
  
  &:hover {
    transform: scale(1.05);
  }
  
  svg {
    width: 100%;
    height: 100%;
  }
}

// 系统标题
.system-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin: 0 0 8px 0;
}

// 系统副标题
.system-subtitle {
  font-size: 14px;
  color: #666;
  margin: 0;
}

// 表单包装器
.form-wrapper {
  margin-bottom: 25px;
}

// 表单输入框样式
.el-input {
  height: 42px;
  margin-bottom: 1px; // 修复边框叠加问题
  
  &:focus-within {
    .el-input__wrapper {
      box-shadow: 0 0 0 1px #FF7A00 inset;
    }
  }
  
  input {
    height: 42px;
    font-size: 14px;
  }
  
  .el-input__icon {
    color: #999;
    
    &.is-focus {
      color: #FF7A00;
    }
  }
}

// 记住密码选项
.remember-me {
  margin: 15px 0;
  
  .el-checkbox__label {
    font-size: 14px;
    color: #666;
  }
  
  .el-checkbox__input.is-checked .el-checkbox__inner {
    background-color: #FF7A00;
    border-color: #FF7A00;
  }
  
  .el-checkbox__input.is-checked + .el-checkbox__label {
    color: #FF7A00;
  }
}

// 登录按钮样式
.el-button--primary {
  background-color: #FF7A00;
  border-color: #FF7A00;
  height: 42px;
  font-size: 16px;
  font-weight: 500;
  transition: all 0.3s ease;
  
  &:hover,
  &:focus {
    background-color: #FF8A20;
    border-color: #FF8A20;
  }
  
  &.is-loading {
    background-color: #FF8A20;
    border-color: #FF8A20;
  }
}

// 验证码区域
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  position: relative;
  
  img {
    cursor: pointer;
    vertical-align: middle;
    border-radius: 6px;
    transition: transform 0.2s ease;
    width: 100%;
    height: 100%;
  }
  
  .code-loading {
    height: 38px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #f5f5f5;
    border: 1px solid #e0e0e0;
    border-radius: 4px;
    color: #666;
    font-size: 14px;
    width: 100%;
    
    i {
      margin-right: 5px;
      animation: rotating 2s linear infinite;
    }
  }
  
  .refresh-btn {
    position: absolute;
    right: 5px;
    top: 50%;
    transform: translateY(-50%);
    padding: 5px;
    min-height: auto;
    z-index: 10;
    
    i {
      font-size: 16px;
      color: #1890ff;
    }
  }
  
  &:hover {
    transform: scale(1.05);
  }
}

.login-code-img {
  height: 42px;
  width: 100%;
  object-fit: cover;
}

// 功能特点区域
.system-features {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
  margin-top: 25px;
  padding-top: 20px;
  border-top: 1px solid #EBEEF5;
}

// 特点项
.feature-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: #606266;
  
  i {
    color: #FF7A00;
    font-size: 16px;
  }
}

// 底部版权信息
.el-login-footer {
  margin-top: 30px;
  text-align: center;
  color: #666;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 0.5px;
  position: relative;
}

// 响应式设计
@media (max-width: 768px) {
  .login-content {
    padding: 15px;
  }
  
  .login-form {
    padding: 25px 20px;
    width: 100%;
    max-width: 400px;
  }
  
  .system-title {
    font-size: 20px;
  }
  
  .system-features {
    grid-template-columns: 1fr;
    gap: 10px;
  }
}

@media (max-height: 600px) {
  .login-container {
    height: auto;
    min-height: 100vh;
    padding: 40px 20px;
  }
  
  .el-login-footer {
    margin-top: 20px;
  }
}

// 全局样式覆盖，确保Element UI组件与主题一致
:deep(.el-input__wrapper:focus-within) {
  box-shadow: 0 0 0 1px #FF7A00 inset;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px #FF7A00 inset;
}

:deep(.el-input__inner:focus) {
  border-color: #FF7A00;
}
</style>
